<?php

/**
 * This is my helper collection, to make coders live easier;)
 *
 * @copyright ragtek
 * @version 1.0.4
 * @package Ragtek/Helper
 */

class Ragtek_Helper_DataWriter
{
    /**
     * Helper for XenForo Thread Datawriter to create a thread
     * @param array $user
     * @param <int> $forumId
     * @param <str> $subject
     * @param <str> $message
     * @return <array> Thread Information
     */
    static public function createThread($user, $forumId, $subject, $message)
    {
        $writer = XenForo_DataWriter::create('XenForo_DataWriter_Discussion_Thread');
        $writer->set('user_id', $user['user_id']);
        $writer->set('username', $user['username']);
        $writer->set('title', $subject);
            $postWriter = $writer->getFirstMessageDw();
            $postWriter->set('message', $message);
        $writer->set('node_id', $forumId);
        $writer->preSave();
        $writer->save();
        return $writer->getMergedData();
    }

    /**
     * Helper for XenForo User Datawriter to create easy an user
     * @param <type> $userName
     * @param <type> $userMail
     * @param <type> $userPassword
     * @param array $additionalData         (   usage: 'gender' => 'male'
     * @return <array>  userinfo
     */
    static public function createUser($userName, $userMail, $userPassword, array $additionalData = array())
    {
        $writer = XenForo_DataWriter::create('XenForo_DataWriter_User');
                $writer->set('username', $userName);
                $writer->set('email', $userMail);
                $writer->setPassword($userPassword);
		$writer->set('user_group_id', XenForo_Model_User::$defaultRegisteredGroupId);
		$writer->set('user_state', 'valid');
                
       foreach($additionalData AS $data => $key)
       {
           $writer->set($data, $key);
       }

	$writer->save();
        return $writer->getMergedData();
    }
}